<template>
	<view class="content">
		<view>
			<view class="headeruser-image">
				<image src="/static/images/index.png">
			</view>
			<view class="main-body">
				<view class="index-box flex-align-center" @click="myMessage">
					<image mode="aspectFill" class="icon tou-img" :src="srcImg"></image>
					<view class="text"><text>我的信息</text></view>
				</view>
				<view class="index-box flex-align-center" @click="scanCode">
					<image class="icon" src="../../static/images/sao.png"></image>
					<view class="text"><text>记录工作</text></view>
				</view>
				<view class="index-box flex-align-center" @click="showToast">
					<image class="icon" src="../../static/images/ji.png"></image>
					<view class="text"><text>工作记录</text></view>
				</view>
				<view class="index-box flex-align-center" @click="openImgShow">
					<image class="icon" src="../../static/images/shezhi.png"></image>
					<view class="text"><text>ICU配置</text></view>
				</view>
				<view class="index-box flex-align-center" @click="workerOn">
					<image class="icon" src="../../static/images/sq.png"></image>
					<view class="text"><text>上班打卡</text></view>
				</view>
				<view class="index-box flex-align-center" @click="workerOff">
					<image class="icon" src="../../static/images/dk1.png"></image>
					<view class="text"><text>下班打卡</text></view>
				</view>
				<!-- <view class="w750 flex-center top-text">
					<h1>@北京小物科技有限公司</h1>
				</view> -->
			</view>
		</view>

		<view>
			<u-modal :show="show" @confirm='infoData' showCancelButton='true' @cancel="closeModal">
				<view class="con-information flex">
					<!-- <view class="w100">
						<u-input disabled='true' v-model="code">
							<u--text text="设备:" slot="prefix" margin="0 3px 0 0" type="tips"></u--text>
						</u-input>
					</view> -->
					<view class="w100">
						<u-input v-model="description">
							<u--text text="图纸编号:" slot="prefix" margin="0 3px 0 0" type="tips"></u--text>
						</u-input>
					</view>
				</view>
			</u-modal>
		</view>

		<view>
			<u-modal :show="showCamer" :title='updateMessage' confirmText='继续拍照' cancelText='完成拍照'
				showCancelButton='true' @confirm="beginCamer" @cancel="okCamer">
			</u-modal>
		</view>

		<view>
			<u-modal :show="showEq" @confirm='infoDataEq' showCancelButton='true' @cancel="closeModalEq">
				<view class="con-information flex">
					<!-- <view class="w100">
						<u-input disabled='true' v-model="code">
							<u--text text="设备:" slot="prefix" margin="0 3px 0 0" type="tips"></u--text>
						</u-input>
					</view> -->
					<view class="w100 flex">
						<text>授权成功！等待管理员审核</text>
						<!-- <u-input v-model="description">
							<u--text text="图纸编号:" slot="prefix" margin="0 3px 0 0" type="tips"></u--text>
						</u-input> -->
					</view>
				</view>
			</u-modal>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showEq: false,
				showCamer: false,
				srcImg: '../../static/ismages/my-on.png',
				show: false,
				showInf: false,
				title: 'Hello123',
				title: '用户协议以及隐私政策',
				code: '',
				name: '',
				address: '',
				describe: '',
				action: '#',
				fileList1: [],
				description: '',
				updateImgShow: false,
				fileListArr1: [],
				updateMessage: '',
				timer: null,
			}
		},
		onLoad() {
			let userData = uni.getStorageSync('userData')
			if (userData == "" || userData == {}) {
				uni.redirectTo({
					url: '../login/login'
				})
			} else {
				this.getTouImage()
			}
		},
		onShow() {
			this.getTouImage()
		},
		mounted() {
			let userData = uni.getStorageSync('userData')
			if (userData == "" || userData == {}) {
				uni.redirectTo({
					url: '../login/login'
				})
			} else {
				this.getTouImage()
			}
		},
		onShareAppMessage: function() {

		},
		methods: {
			// 上班打卡
			workerOn() {
				let that = this
				this.$post('/admin/attendance/punchIn', {}).then(res => {
					console.log(res)
					debugger
					if (res.code == 200) {
						setTimeout(() => {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('上班打卡成功');
							// 通知验证码组件内部开始倒计时
						}, 1000);
					} else {
						setTimeout(() => {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast(res.message);
							// 通知验证码组件内部开始倒计时
						}, 1000);
					}
				})
				/* uni.openBluetoothAdapter({
					success: function(res) {
						console.log(res, "初始蓝牙成功")
						// 蓝牙初始化成功后搜索设备
						that.timer = setInterval(() => {
							uni.startBluetoothDevicesDiscovery({
								success(resd) {
									console.log(resd, "搜索附近蓝牙设备成功")
									uni.getBluetoothDevices({
										success: function(res) {
											uni.hideLoading();
											console.log(res)
											res.devices.forEach(item => {
												if (item.localName ==
													'ICU21' || item.name ==
													'ICU21') {
													setTimeout(() => {
														uni
															.hideLoading();
														// 这里此提示会被this.start()方法中的提示覆盖
														uni.$u
															.toast(
																'打卡成功！'
															);
														clearInterval
															(that
																.timer
															)
														// 通知验证码组件内部开始倒计时
														this.$refs
															.uCode
															.start();
													}, 1000);
												} else {
													uni.$u.toast('等待中！');
												}
											})
											//that.searchLanY(res.devices)
										},
										fail: function() {
											console.log("搜索蓝牙设备失败")
											setTimeout(() => {
												uni.hideLoading();
												// 这里此提示会被this.start()方法中的提示覆盖
												uni.$u.toast('搜索失败！');
												clearInterval(that.timer)
												// 通知验证码组件内部开始倒计时
												this.$refs.uCode.start();
											}, 1000);
										}
									})
								},
								fail(err) {
									console.log('搜索失败')
									console.error(err)
									that.load = false
								}
							})
						}, 2000)
					},
					fail: function(msg) {
						console.log(msg, "初始化蓝牙失败")
						that.load = false
					}
				}) */
			},
			// 下班打卡
			workerOff() {
				let that = this
				this.$post('/admin/attendance/punchOut', {}).then(res => {
					console.log(res)
					debugger
					if (res.code == 200) {
						setTimeout(() => {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('下班打卡成功');
							// 通知验证码组件内部开始倒计时
						}, 1000);
					} else {
						setTimeout(() => {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast(res.message);
							// 通知验证码组件内部开始倒计时
						}, 1000);
					}
				})
			},
			// 关闭输入编号窗口
			closeModal() {
				this.show = false
			},
			// 上传信息
			showToast() {
				uni.navigateTo({
					url: '/pages/userlist/userlist'
				});
			},
			// 扫码
			scanCode() {
				const _this = this;
				_this.show = true
				/* uni.scanCode({
					success(res) {
						_this.code = res.result;
						_this.show = true
					}
				}); */
			},
			// 授权信息
			empowerAdd() {
				let _this = this
				uni.scanCode({
					success(res) {
						console.log(res.result)
						if (res.result === "503") {
							_this.showEq = true
							_this.doLogin()
						}
					}
				});
			},
			// 登录后台
			doLogin() {
				uni.request({
					url: 'https://uu-iot.com/gateway/api/user/doLogin', //仅为示例，并非真实接口地址。
					method: 'POST',
					data: {
						username: 'zhangsan',
						password: '123456'
					},
					header: {
						//'custom-header': 'hello' //自定义请求头信息
					},
					success: (res) => {
						console.log(res.data);
						// 授权人员信息
						uni.request({
							url: 'https://uu-iot.com/gateway/api/user/saveUser', //仅为示例，并非真实接口地址。
							method: 'POST',
							data: {
								name: uni.getStorageSync("username"),
								address: '泰豪智能大厦B座503',
								code: '503',
								orgId: 41
							},
							header: {
								//'custom-header': 'hello' //自定义请求头信息
								"Authorization": res.data.data
							},
							success: (res) => {
								console.log(res.data);
							}
						});
					}
				});
			},
			// 打开相机拍照
			cameraImage() {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['camera'], // 这要注意，camera掉拍照，album是打开手机相册
					success: (res) => {
						const tempFilePaths = res.tempFilePaths;
						uni.uploadFile({
							url: 'https://www.xiaowu-iot.com/api/admin/file/upload',
							filePath: res.tempFilePaths[0],
							name: 'file',
							header: {
								Authorization: uni.getStorageSync('token')
							},
							formData: {
								indexId: uni.getStorageSync('userId'),
								//	deviceName: this.code,
								description: this.description,
								file: res.tempFilePaths[0]
							},
							success: (resd) => {
								let data = JSON.parse(resd.data)
								this.updateMessage = `${data.message}是否继续拍照`
								/* this.code = ''
								this.description = '' */
								uni.showToast({
									icon: 'success',
									title: data.messages
								})
								this.showCamer = true
							},
							error: (reserd) => {
								uni.showToast({
									icon: 'error',
									title: '服务器异常请稍后重试...'
								})
							}
						})
					}
				});
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
					})
				})
			},
			// 确认认证内容框
			agreeConfirm() {
				this.show = false
			},
			// 退出认证
			signOut() {
				uni.navigateTo({
					url: '../login/login'
				})
			},
			// 模态框确认按钮
			infoData() {
				if (this.description) {
					this.show = false
					this.cameraImage()
				} else {
					uni.$u.toast('请填写编号')
				}
			},
			// 个人信息
			myMessage() {
				uni.navigateTo({
					url: '../information/information'
				})
			},
			// 继续拍照
			beginCamer() {
				this.cameraImage()
			},
			// 完成拍照
			okCamer() {
				this.showCamer = false
				//uni.$u.toast('全部信息上传成功！')
				this.code = ''
				this.description = ''
			},
			// 回显头像
			getTouImage() {
				let userData = uni.getStorageSync('userData')
				this.$get('/admin/file/getOutsideChain', {
					fileName: userData.profilePicture,
				}).then(res => {
					this.srcImg = res.data.outsideChain
				})
			},
			openImgShow() {
				uni.navigateTo({
					url: '/pages/configure/configure'
				})
			},
			infoDataEq() {
				this.showEq = false
			},
			closeModalEq() {
				this.showEq = false
			},
		}
	}
</script>

<style lang="scss" scoped>
	.text {
		color: #000000;
		font-size: 40rpx;
		font-weight: 800;
		margin-left: 50rpx;
	}

	.top-text {
		position: fixed;
		left: 0px;
		bottom: 40px;
		font-size: 35rpx;
		color: #919194;
	}

	.tou-img {
		border-radius: 50%;
	}

	.img-tou {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		background-color: red;
		margin-left: 35rpx;
	}

	.icon {
		width: 100rpx;
		height: 100rpx;
		margin-left: 35rpx;
	}

	.index-box {
		width: 700rpx;
		height: 150rpx;
		background-color: #ffffff;
		border-radius: 25rpx;
		margin-left: 25rpx;
		margin-top: 35rpx;
	}

	.flex {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-wrap: wrap;
	}

	.content {
		position: relative;
		background-color: #f5f5f5;
		overflow: auto;
	}


	.headeruser-image {
		width: 750rpx;
		height: 100%;
		position: absolute;
		z-index: 0;
	}

	.images-content {
		width: 600rpx;
		height: 600rpx;
		margin-top: 90rpx;
	}

	.btn-update {
		width: 600rpx;
		height: 90rpx;
		margin-top: 130rpx;
	}

	.headeruser-image>image {
		width: 100%;
		height: 100%;
	}

	.main-body {
		width: 750rpx;
		position: absolute;
		z-index: 1;
		padding-top: 60%;
		overflow: hidden;
	}

	.scan-code {
		width: 330rpx;
		height: 330rpx;
		position: absolute;
		left: 210rpx;
		top: 440rpx;
		border-radius: 30px;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.text-tit {
		position: absolute;
		width: 750rpx;
		text-align: center;
		top: 78%;
	}

	.title {
		font-size: 35rpx;
		color: #8f8f94;
	}

	.w100 {
		width: 100%;
	}

	.content-text {
		font-size: 30rpx;
		color: #777777;
	}

	.con-information {
		width: 500rpx;
		height: 200rpx;
	}

	.impot {
		overflow: auto;
		display: flex;
	}
</style>